import {
  _decorator,
  Collider2D,
  Component,
  Contact2DType,
  Director,
  director,
  EventKeyboard,
  Input,
  input,
  instantiate,
  IPhysics2DContact,
  KeyCode,
  Label,
  Node,
  Prefab,
} from 'cc'
const { ccclass, property } = _decorator

@ccclass('GameRoot')
export class GameRoot extends Component {
  @property(Node) player: Node
  @property(Node) hensRoot: Node
  @property(Node) eggsRoot: Node
  @property(Prefab) eggPrefab: Prefab
  @property(Label) scoreLabel: Label
  @property(Label) hpLabel: Label

  playerPosIndex = 0
  hensPosXArr = []
  score = 0
  hp = 3

  initData() {
    for (let i = 0; i < this.hensRoot.children.length; i++) {
      const hen = this.hensRoot.children[i]
      this.hensPosXArr[i] = hen.position.x
    }
    this.renderPlayerPos()
  }

  start() {
    this.initData()
    this.openInputEvent()
    this.createEggs()
    this.openCollider2DEvent()
    this.renderLabel()
  }

  openCollider2DEvent() {
    const comp = this.player.getComponent(Collider2D)
    comp.on(
      Contact2DType.BEGIN_CONTACT,
      (selfCollider: Collider2D, otherCollider: Collider2D, contact: IPhysics2DContact) => {
        console.log('碰到鸡蛋，分数+1')
        director.once(
          Director.EVENT_AFTER_PHYSICS,
          () => {
            otherCollider.node.destroy()
          },
          this
        )
        this.score++
        this.renderLabel()
      },
      this
    )
  }

  createEggs() {
    this.schedule(this.createOneEgg, 2)
  }

  createOneEgg() {
    const randomIndex = Math.floor(Math.random() * this.hensPosXArr.length)
    const egg = instantiate(this.eggPrefab)
    this.eggsRoot.addChild(egg)
    egg.setPosition(this.hensPosXArr[randomIndex], this.hensRoot.position.y)
  }

  openInputEvent() {
    input.on(Input.EventType.KEY_DOWN, this.onKeyDown, this)
  }

  movePlayer(dir: 1 | -1) {
    this.playerPosIndex += dir
    if (this.playerPosIndex < 0) {
      this.playerPosIndex = 4
    }
    if (this.playerPosIndex > 4) {
      this.playerPosIndex = 0
    }
    this.renderPlayerPos()
  }

  renderLabel() {
    this.scoreLabel.string = `得分：${this.score}分`
    this.hpLabel.string = `生命：${this.hp}`
  }

  renderPlayerPos() {
    const x = this.hensPosXArr[this.playerPosIndex]
    const y = this.player.position.y
    this.player.setPosition(x, y)
  }

  onKeyDown(event: EventKeyboard) {
    switch (event.keyCode) {
      case KeyCode.KEY_A:
        // 左移
        this.movePlayer(-1)
        break
      case KeyCode.KEY_D:
        // 右移
        this.movePlayer(1)
        break
    }
  }

  checkGameOver() {
    if (this.hp <= 0) {
      console.log('游戏结束')
    }
  }

  update(deltaTime: number) {
    for (let i = 0; this.eggsRoot.children.length; i++) {
      const egg = this.eggsRoot.children[i]

      const x = egg.position.x
      const y = egg.position.y - 150 * deltaTime
      egg.setPosition(x, y)

      if (y < -600) {
        egg.destroy()
        this.hp--
        this.renderLabel()
        this.checkGameOver()
      }
    }
  }
}
